{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "isInteractiveWindowMessageCell": true
   },
   "source": [
    "已连接到 env_py38 (Python 3.8.18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   分拣中心          日期  小时   货量\n",
      "0  SC54   2023/11/6  19  784\n",
      "1  SC54  2023/11/21   5   72\n",
      "2  SC54  2023/11/30  12  209\n",
      "3  SC54  2023/11/13   1  520\n",
      "4  SC54   2023/11/4  12  278\n",
      "\n",
      " 分拣中心    object\n",
      "日期      object\n",
      "小时       int64\n",
      "货量       int64\n",
      "dtype: object\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 33281 entries, 0 to 33280\n",
      "Data columns (total 4 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   分拣中心    33281 non-null  object\n",
      " 1   日期      33281 non-null  object\n",
      " 2   小时      33281 non-null  int64 \n",
      " 3   货量      33281 non-null  int64 \n",
      "dtypes: int64(2), object(2)\n",
      "memory usage: 1.0+ MB\n",
      "\n",
      " None\n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "fujian2_df = pandas.read_csv('D:\\ProgramFile2_OR\\Python_study\\mathorcup2024C\\附件\\附件2.csv',encoding = 'utf-8')\n",
    "print(fujian2_df.head())\n",
    "print('\\n',fujian2_df.dtypes)\n",
    "print('\\n',fujian2_df.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  分拣中心         日期  小时    货量\n",
      "0  SC1 2023-11-01   0  6059\n",
      "1  SC1 2023-11-01   1  4810\n",
      "2  SC1 2023-11-01   2  5457\n",
      "3  SC1 2023-11-01   3  5045\n",
      "4  SC1 2023-11-01   4  4015\n",
      "5  SC1 2023-11-01   5  2295\n",
      "6  SC1 2023-11-01   6  2235\n",
      "7  SC1 2023-11-01   7  3909\n",
      "8  SC1 2023-11-01   8  4526\n",
      "9  SC1 2023-11-01   9  4061\n"
     ]
    }
   ],
   "source": [
    "fujian2_df['日期'] = pandas.to_datetime(fujian2_df['日期'])\n",
    "strSplit_num = [int(item[2:]) for item in fujian2_df['分拣中心']]\n",
    "fujian2_df['asSort'] = strSplit_num\n",
    "data_group = fujian2_df.sort_values(by = 'asSort').reset_index(drop = True)\n",
    "data_fujian2 = data_group.groupby('asSort').apply(lambda x:x.sort_values(by = '日期')\\\n",
    "                                                  .reset_index(drop = True)).reset_index(drop = True)\n",
    "data_fujian2 = data_fujian2.drop('asSort',axis = 1)\n",
    "data_sorted = data_fujian2.groupby(by = ['分拣中心','日期']).apply(lambda x: x.sort_values(by = '小时')\\\n",
    "                                                             .reset_index(drop = True)).reset_index(drop = True)\n",
    "\n",
    "# strSplit_num2 = [int(item[2:]) for item in fujian2_df['分拣中心']]\n",
    "# data_sorted['asSort'] = strSplit_num2\n",
    "# data_final = data_sorted.sort_values(by = 'asSort').reset_index(drop = True)\n",
    "# data_final = data_final.drop('asSort',axis = 1)\n",
    "\n",
    "print(data_sorted.head(n=10))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_sorted.to_csv(r\"D:\\ProgramFile2_OR\\Python_study\\mathorcup2024C\\sortData_fujian2.csv\",index = False,encoding = 'utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      分拣中心          日期  小时    货量\n",
      "720   SC10  2023-11-01   0  1917\n",
      "721   SC10  2023-11-01   1  1514\n",
      "722   SC10  2023-11-01   2   955\n",
      "723   SC10  2023-11-01   3  2089\n",
      "724   SC10  2023-11-01   4  2915\n",
      "...    ...         ...  ..   ...\n",
      "1195  SC10  2023-11-20  22  4961\n",
      "1196  SC10  2023-11-20  23  4407\n",
      "1197  SC10  2023-11-21   0  4419\n",
      "1198  SC10  2023-11-21   1  1586\n",
      "1199  SC10  2023-11-21   2  1959\n",
      "\n",
      "[480 rows x 4 columns]\n",
      "     分拣中心          日期  小时   货量  Asort\n",
      "720   SC2  2023-11-13  18  858      2\n",
      "721   SC2  2023-11-13  19  229      2\n",
      "722   SC2  2023-11-13  14  713      2\n",
      "723   SC2  2023-11-13  15  674      2\n",
      "724   SC2  2023-11-13  16  870      2\n",
      "...   ...         ...  ..  ...    ...\n",
      "1195  SC2  2023-11-30   7  203      2\n",
      "1196  SC2  2023-11-30   8  387      2\n",
      "1197  SC2  2023-11-30   9  461      2\n",
      "1198  SC2  2023-11-30  10  524      2\n",
      "1199  SC2  2023-11-30  11  476      2\n",
      "\n",
      "[480 rows x 5 columns]\n"
     ]
    }
   ],
   "source": [
    "data_df = pandas.read_csv('D:\\ProgramFile2_OR\\Python_study\\mathorcup2024C\\sortData_fujian2.csv',encoding  ='utf-8')\n",
    "print(data_df.iloc[30*24:50*24,:])\n",
    "dataAsort = [int(item[2:]) for item in data_df['分拣中心']]\n",
    "data_df['Asort'] = dataAsort\n",
    "data_df = data_df.sort_values('Asort').reset_index(drop = True)\n",
    "print(data_df.iloc[30*24:50*24,:])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['SC1' 'SC10' 'SC12' 'SC14' 'SC15' 'SC16' 'SC17' 'SC18' 'SC19' 'SC2'\n",
      " 'SC20' 'SC21' 'SC22' 'SC23' 'SC24' 'SC25' 'SC26' 'SC27' 'SC28' 'SC29'\n",
      " 'SC3' 'SC30' 'SC31' 'SC32' 'SC34' 'SC35' 'SC36' 'SC37' 'SC38' 'SC39'\n",
      " 'SC4' 'SC40' 'SC41' 'SC43' 'SC44' 'SC46' 'SC47' 'SC48' 'SC49' 'SC5'\n",
      " 'SC51' 'SC52' 'SC53' 'SC54' 'SC55' 'SC56' 'SC57' 'SC58' 'SC6' 'SC60'\n",
      " 'SC61' 'SC63' 'SC66' 'SC68' 'SC7' 'SC8' 'SC9']\n"
     ]
    }
   ],
   "source": [
    "dataStr = data_df['分拣中心'].unique()\n",
    "print(dataStr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1, 10, 12, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 34, 35, 36, 37, 38, 39, 4, 40, 41, 43, 44, 46, 47, 48, 49, 5, 51, 52, 53, 54, 55, 56, 57, 58, 6, 60, 61, 63, 66, 68, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "print(type(dataStr))\n",
    "lisNum_str = [int(item[2:]) for item in dataStr]\n",
    "print(lisNum_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#第四章，数据组合-连接-添加行或列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我的思路可能从一开始就错了，实际上我需要的仅仅是将那些数据由一个文件分裂成多个文件（每一文件都是一个分拣中心的数据）\n",
    "进行处理和透视，而对整体的分析仍需要将它们在单个文件中进行转换。而对于单个文件的处理，我现在有三种想法，其一是找到\n",
    "函数的组合进行使用；其二，既然没有掌握那种函数处理技巧，那么我可以采用一种稍微笨拙的办法，即每次只处理一个问题，然后\n",
    "将它保存为新的文件，这样拆分处理，减少耦合性；其三，适合更为细致的观察，需要更多的细节，利用每一个分类的索引，分块处理。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
